package com.qdxht.dispenser.view;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.WindowManager;
import androidx.appcompat.app.AlertDialog;

import com.qdxht.dispenser.R;

/**
 * Created by SEA
 */
public class DialogUtil {

    /**
     * 显示消息提示对话框，如果某个参数传空，则不会显示相关view
     *
     * @param context
     * @param title         标题
     * @param msg           内容
     * @param postiveBtnTxt 确定按钮文字
     * @param posListener   确定点击事件
     * @param negtiveBtnTxt 取消按钮文字
     * @param negListener   取消按钮点击事件
     */
    public static void showMessageDialog(Context context, String title, String msg, String postiveBtnTxt,
                                         DialogInterface.OnClickListener posListener, String negtiveBtnTxt, DialogInterface.OnClickListener negListener) {
        AlertDialog builder = new AlertDialog.Builder(context).create();
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }
        if (!TextUtils.isEmpty(msg)) {
            builder.setMessage(msg);
        }
        if (!TextUtils.isEmpty(postiveBtnTxt)) {
            builder.setButton(DialogInterface.BUTTON_POSITIVE, postiveBtnTxt, posListener);
        }
        if (!TextUtils.isEmpty(negtiveBtnTxt)) {
            builder.setButton(DialogInterface.BUTTON_NEGATIVE, negtiveBtnTxt, negListener);
        }
//        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialogInterface) {
//                AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources());
//            }
//        });
        try {
//            AutoSizeCompat.cancelAdapt(activity);
            builder.show();
        } catch (WindowManager.BadTokenException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示消息提示对话框，如果某个ID传0，则不会显示相关view
     *
     * @param context
     * @param titleId         标题ID
     * @param msgId           消息ID
     * @param postiveBtnTxtId 确定文字 ID
     * @param posListener     确定点击事件
     * @param negtiveBtnTxtId 取消文字ID
     * @param negListener     取消点击事件
     */
    public static void showMessageDialog(Context context, int titleId, int msgId, int postiveBtnTxtId,
                                         DialogInterface.OnClickListener posListener, int negtiveBtnTxtId, DialogInterface.OnClickListener negListener) {
        AlertDialog builder = new AlertDialog.Builder(context).create();
        if (titleId != 0) {
            builder.setTitle(titleId);
        }
        if (msgId != 0) {
            builder.setMessage(context.getResources().getString(msgId));
        }
        if (postiveBtnTxtId != 0) {
            builder.setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(postiveBtnTxtId), posListener);
        }
        if (negtiveBtnTxtId != 0) {
            builder.setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(negtiveBtnTxtId), negListener);
        }

        try {
            builder.show();
        } catch (WindowManager.BadTokenException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示错误提示框
     *
     * @param context
     * @param msgId
     * @param postiveBtnTxtId
     * @param posListener
     * @param negtiveBtnTxtId
     * @param negListener
     */
    public static void showErrorMessageDialog(Context context, int msgId, int postiveBtnTxtId,
                                              DialogInterface.OnClickListener posListener, int negtiveBtnTxtId, DialogInterface.OnClickListener negListener) {
        showMessageDialog(context, R.string.dialog_title, msgId, postiveBtnTxtId, posListener, negtiveBtnTxtId, negListener);
    }

    /**
     * 显示错误提示框
     *
     * @param context
     * @param msg
     * @param postiveBtnTxt
     * @param posListener
     * @param negtiveBtnTxt
     * @param negListener
     */
    public static void showErrorMessageDialog(Context context, String msg, String postiveBtnTxt,
                                              DialogInterface.OnClickListener posListener, String negtiveBtnTxt, DialogInterface.OnClickListener negListener) {
        showMessageDialog(context, context.getResources().getString(R.string.dialog_title), msg, postiveBtnTxt, posListener, negtiveBtnTxt, negListener);
    }

    public static void showErrorMessageDialog(Context context, String msg, String postiveBtnTxt, DialogInterface.OnClickListener posListener) {
        showMessageDialog(context, context.getResources().getString(R.string.dialog_title), msg, postiveBtnTxt, posListener, context.getResources().getString(R.string.dialog_cancel), null);
    }

    public static void showErrorMessageDialog(Context context, String msg, DialogInterface.OnClickListener posListener) {
        showMessageDialog(context, context.getResources().getString(R.string.dialog_title), msg,  context.getResources().getString(R.string.dialog_confirm), posListener, context.getResources().getString(R.string.dialog_cancel), null);
    }

}
